#pragma once

#include <iosfwd>
#include <vector>
namespace std
{
	typedef basic_stringstream<TCHAR, char_traits<TCHAR>,allocator<TCHAR> > tstringstream;
}

namespace cpputil
{
	//noncopyable
	class noncopyable
	{
	protected:
		noncopyable() {}
		~noncopyable() {}
	private:  // emphasize the following members are private
		noncopyable( const noncopyable& );
		const noncopyable& operator=( const noncopyable& );
	};

	//auto ptr set function : delete ptr object, set ptr to 0/new ptr
	template<class T>
	void autoptr_reset(T*& p, T* newp=0)
	{
		if(p)
			delete p;
		p = newp;
	}


	template<class T>
	void deleteMapItem(T& map)
	{
		for(T::iterator itr = map.begin();itr!=map.end();++itr)
		{
			delete itr->second;
		}
		map.clear();
	}

	template<class T>
	void deleteVectorItem(T& vec)
	{
		for(T::iterator itr = vec.begin();itr!=vec.end();++itr)
		{
			delete *itr;
		}
		vec.clear();
	}

	template<class T>
	void strConvert(const CString& s, T& val)
	{
		std::tstringstream str(s.GetString());	
		str >> val;
		return !str.fail() && str.eof();
	}

	template<class T> 
	class DataPool
	{
	public:
		~DataPool(){ deleteVectorItem(vData); }
		T* defaultAllocate()
		{ 
			T* pD = new T();
			if(pD)
				vData.push_back(pD);
		}
		void addData(T* pD){
			if(hasData(pD))
				return;
			vData.push_back(pD);
		}

		bool hasData(T* pD)const{
			return std::find(vData.begin(),vData.end(),pD)!=vData.end();
		}
		std::vector<T*> vData;
	};


}


#define DELETE_EXCEPTION(e) do { if(e) { e->Delete(); } } while (0)
#define NO_CPP_EXCEPTION(expr)
